#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "include/vector.h"

// 扩容，为了统一扩容缩容代码，新容量通过newCap参数传递进来
static void ensureCap(Vector* vector, int newCap) {
    vector->elems = realloc(vector->elems, newCap * sizeof(int));
    vector->cap = newCap; // size 没有变化
    assert(vector->elems != NULL); 
}

static void checkForAdd(Vector* vector, int index) {
    if (index < 0 || index > vector->size) {
        printf("index must be in [%d, %d]: %d\n", 0, vector->size); 
        exit(EXIT_FAILURE);
    }
}

static void checkForUpdate(Vector* vector, int index) {
    if (index < 0 || index >= vector->size) {
        printf("index must be in [%d, %d): %d\n", 0, vector->size); 
        exit(EXIT_FAILURE);
    }
}

// 向量操作
// 构造函数
void VectorNew(Vector* vector) {
    vector->size = 0; 
    vector->cap = 4; 
    vector->elems = (int*)malloc(vector->cap * sizeof(int)); 
    assert(vector->elems != NULL); 
}

// 添加
void VectorAddFirst(Vector* vector, int elem) { VectorAdd(vector, 0, elem); } 
void VectorAdd(Vector* vector, int index, int elem) {
    // printf("index = %d, size = %d\n", index, vector->size); 
    checkForAdd(vector, index); 
    if (vector->size == vector->cap) ensureCap(vector, vector->cap * 2); 
    for (int i = vector->size - 1; i >= index; i--)
        vector->elems[i + 1] = vector->elems[i]; 
    vector->elems[index] = elem;
    vector->size++; 
}
void VectorAddLast(Vector* vector, int elem) { VectorAdd(vector, vector->size, elem); } 

// 查询
int VectorGetFirst(Vector* vector) { return VectorGet(vector, 0); }
int VectorGet(Vector* vector, int index) {
    checkForUpdate(vector, index);
    return vector->elems[index];
}
int VectorGetLast(Vector* vector) { return VectorGet(vector, vector->size - 1); }

// 修改
int VectorSet(Vector* vector, int index, int elem) {
    checkForUpdate(vector, index); 
    int old = vector->elems[index];
    vector->elems[index] = elem; 
    return old; 
} 

// 删除
int VectorRemoveFirst(Vector* vector) { return VectorRemove(vector, 0); }
int VectorRemove(Vector* vector, int index) {
    checkForUpdate(vector, index);
    int old = vector->elems[index];
    for (int i = index + 1; i < vector->size; i++) 
        vector->elems[i - 1] = vector->elems[i]; 
    vector->size--;

    if (vector->size == vector->cap / 4 && vector->cap / 2 != 0) 
        ensureCap(vector, vector->cap / 2); 
    return old; 
}
int VectorRemoveLast(Vector* vector) { return VectorRemove(vector, vector->size - 1); }

// 查找
int VectorFind(Vector* vector, int elem) {
    for(int i = 0; i < vector->size; i++) {
        if (vector->elems[i] == elem) 
            return i; // 索引
    }
    return -1; // 没找到
} 

// 判断
int VectorContains(Vector* vector, int elem) {
    int index = VectorFind(vector, elem);
    if (index != -1)
        return 1; // 存在
    return -1; // 不存在
} 

// 打印
void VectorString(Vector* vector) {
    printf("Vector(size = %d, cap = %d):\n", vector->size, vector->cap); 
    printf(" > [");
    for (int i = 0; i < vector->size; i++) {
        printf("%d", vector->elems[i]);
        if (i != vector->size - 1)
            printf(", ");
    }
    printf("]\n"); 
} 

int VectorSize(Vector* vector) { return vector->size; }
int VectorIsEmpty(Vector* vector) { return vector->size == 0; }

// 析构函数
void VectorDispose(Vector* vector) {
    if (vector->elems)
        free(vector->elems);  // 释放掉申请的堆内存
}



